package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/15 13:30
 */
public class C476 {

    // https://leetcode-cn.com/problems/number-complement/
    // 从第一个1开始, 往后0变1, 1变0
    public int findComplement(int num) {
        int n = 31;
        for (; n >= 0 && ((num & (1 << n)) == 0); n--) ;
        n = 31 - n;
        return ~(num << n) >> n;
    }


    // for test
    public static void main(String[] args) {
        C476 test = new C476();
        int num = 1;
        System.out.println(test.findComplement(num));
    }

}
